<?php
declare (strict_types = 1);

namespace app\index\validate;

use think\Validate;

#[class('User', 'user validate')]
class User extends Validate
{

    protected $rule = [
        'username'  => 'require|max:20',
        'oldpasswd' => 'require|max:32',
        'passwd'    => 'require|max:32',
        'repasswd'  => 'require|max:32|confirm:passwd',
        'nickname'  => 'require|max:20',
        'birth'     => 'require|max:10|egt:0',
        'phone'     => 'require|max:11|number|unique:User',
        'email'     => 'require|max:32|email|unique:User',
        'sex'       => 'require|max:2',
    ];

    protected $message = [
        'username.require'  => '用户名不能为空',
        'username.max'      => '用户名最多不能超过20个字符',
        'username.unique'   => '账号已存在',

        'oldpasswd.require' => '旧密码不能为空',
        'oldpasswd.max'     => '旧密码最多不能超过32个字符',

        'passwd.require'    => '密码不能为空',
        'passwd.max'        => '密码最多不能超过32个字符',

        'repasswd.require'  => '确认密码不能为空',
        'repasswd.max'      => '确认密码最多不能超过32个字符',
        'repasswd.confirm'  => '确认密码不一致',

        'nickname.require'  => '昵称不能为空',
        'nickname.max'      => '用户名最多不能超过20个字符',

        'birth.require'     => '生日不能为空',
        'birth.max'         => '生日最多不能超过10个字符',
        'birth.egt'         => '请选择生日',

        'phone.require'     => '手机号不能为空',
        'phone.max'         => '手机号最多不能超过11个字符',
        'phone.number'      => '手机号输入不合法',

        'email.require'     => '邮箱不能为空',
        'email.max'         => '邮箱最多不能超过32个字符',
        'email.email'       => '邮箱输入不合法',

        'sex.require'       => '性别不能为空',
        'sex.max'           => '性别最多不能超过2个字符',
    ];

    #[function('sceneReg', 'register')]
    public function sceneReg()
    {
        return $this->only(['username', 'passwd', 'repasswd', 'sex', 'nickname'])->append('username', 'unique:user');
    }

    #[function('sceneLogin', 'login')]
    public function sceneLogin()
    {
        return $this->only(['username', 'passwd']);
    }

    #[function('sceneEdit', 'edit')]
    public function sceneEdit()
    {
        return $this->only(['nickname', 'birth', 'sex']);
        return $this->only(['nickname', 'birth', 'phone', 'email', 'sex']);
    }

    #[function('scenePasswd', 'modify passwd')]
    public function scenePasswd()
    {
        return $this->only(['passwd', 'repasswd']);
    }
}
